package com.ruoyi.mdkt.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.mdkt.config.LocalDateTimeConfig;
import com.ruoyi.mdkt.entity.Goods;
import com.ruoyi.mdkt.entity.Pet;
import com.ruoyi.mdkt.service.PetService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gwl
 * @since 2022-10-22
 */
@CrossOrigin

@Api(tags = "宠物管理")
@RestController
@RequestMapping("/mdkt/pet")
public class PetController extends BaseController {

    @Autowired
    private PetService petService;

    /**
     * 获取宠物信息
     * @return
     */
    @GetMapping("/getPet")
    public R<Pet> getPet(){
        Long userId = getUserId();
        LambdaQueryWrapper<Pet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pet::getUserId,userId);
        Pet one = petService.getOne(wrapper);
        if (one == null){
            return R.fail();
        }
        one.setLastLogonTime(LocalDateTime.now());
        petService.updateById(one);
        return R.ok(one);
    }

    /**
     * 宠物使用物品
     * @param goodsId
     * @return
     */
    @PutMapping("/updatePet/{goodsId}")
    public R<String> updatePet(@PathVariable Long goodsId){
        Boolean isOk = petService.updatePet(goodsId, getUserId());
        if (isOk){
            return R.ok("喂食成功");
        }
        return R.fail("背包物品不足");
    }




}

